package com.ty.bigevent.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ty.bigevent.model.dto.UserRegisterRequest;
import com.ty.bigevent.model.entry.User;
import com.ty.bigevent.service.UserService;
import com.ty.bigevent.mapper.UserMapper;
import com.ty.bigevent.utils.JwtUtil;
import com.ty.bigevent.utils.Result;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

/**
* @author ong char
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2025-09-27 01:06:19
*/
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    private UserMapper userMapper;
    private static final String Salt = "ty_big_event";
    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public void register(String username, String password) {
//        // username为非空5-16为字符
//        if (username == null || username.length() < 5 || username.length() > 16){
//            throw new RuntimeException("用户名长度必须在5-16位之间");
//        }
//        if (password == null || password.length() < 5 || password.length() > 16){
//            throw new RuntimeException("密码长度必须在5-16位之间");
//        }
        // 5393554e94bf0eb6436f240a4fd71282
        String newPassword = DigestUtil.md5Hex(password+Salt);

        userMapper.register(username, newPassword);
    }

    @Override
    public String login(String username, String password) {
        User user = findByUsername(username);
        String newPassword = DigestUtil.md5Hex(password+Salt);
        if (!user.getPassword().equals(newPassword)){
            throw new RuntimeException("密码错误");
        }
        // 生成jwt令牌
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());
        claims.put("username", username);
        claims.put("password", password);
        return JwtUtil.genToken(claims);
    }

    @Override
    public void updateUser(User user) {
        user.setUpdate_time(new Date());
        userMapper.updateUser(user);
    }

    @Override
    public void updateUserAvatar(Integer userId,String avatarUrl) {
        userMapper.updateUserAvatarUrl(userId,avatarUrl);
    }

    @Override
    public void updatePassword(Integer userId,String oldPassword, String newPassword) {
        String newOldPassword = DigestUtil.md5Hex(oldPassword+Salt);
        String newNewPassword = DigestUtil.md5Hex(newPassword+Salt);
        //比较用户旧密码是否一致
        User user = userMapper.findUserById(userId);
        String password = user.getPassword();
        if (!password.equals(newOldPassword)){
            throw new RuntimeException("旧密码与原密码不一致");
        }
        userMapper.updateUserPassword(userId,newNewPassword);
    }
}




